package com.niodata.dp.monitor.components.task.controller;

import com.google.common.base.Strings;
import com.niodata.dp.monitor.components.task.service.TaskStatisticService;
import com.niodata.dp.monitor.sharelib.entity.base.BaseResult;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskExecCount;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskFailedObj;
import com.niodata.dp.monitor.sharelib.entity.monitor.task.TaskStaticObj;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/monitor/statistic/task")
public class TaskStatisticController {

  private Logger logger = Logger.getLogger(getClass());

  @Autowired
  private TaskStatisticService taskStatisticService;

  /**
   * getTaskNum .
   *
   * @param ownerId ownerId
   * @param groupIds groupIds
   * @param condition condition
   * @return count map
   */
  @RequestMapping(value = "/exec/count", method = RequestMethod.GET)
  public BaseResult getTaskNum(
        @RequestParam(value = "ownerId") long ownerId,
        @RequestParam(value = "groupIds") String groupIds,
        @RequestParam(value = "condition") String condition
  ) {
    List<Long> ids = new ArrayList<>();
    if (!Strings.isNullOrEmpty(groupIds)) {
      ids = Arrays.stream(groupIds.trim().split(",")).map(str -> {
        return Long.valueOf(str);
      }).collect(Collectors.toList());
    }

    Date now = new Date();
    Calendar startDate = Calendar.getInstance();
    startDate.setTime(now);
    startDate.set(Calendar.MINUTE, 00);
    startDate.set(Calendar.SECOND, 00);

    Date startTime = null;

    //HOUR 代表统计12小时
    if ("HOUR".equals(condition)) {
      startDate.add(Calendar.HOUR_OF_DAY, -12);
      startTime = startDate.getTime();
    } else if ("DAY".equals(condition)) {
      startDate.add(Calendar.HOUR_OF_DAY, -24);
      startTime = startDate.getTime();
    } else if ("WEEK".equals(condition)) {
      startDate.add(Calendar.HOUR_OF_DAY, -168);
      startTime = startDate.getTime();
    }

    Calendar endDate = Calendar.getInstance();
    endDate.setTime(now);
    endDate.add(Calendar.HOUR_OF_DAY, -1);
    endDate.set(Calendar.MINUTE, 59);
    endDate.set(Calendar.SECOND, 59);
    Date endTime = endDate.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
    logger.info(sdf.format(startTime) + "--->" + sdf.format(endTime));
    Map<String, Integer> res = taskStatisticService.getTaskCount(ownerId, ids,
          sdf.format(startTime), sdf.format(endTime));

    List data = new ArrayList();
    data.add(res);
    BaseResult result = new BaseResult<>();
    result.setData(data);
    result.setSuccess(true);
    return result;
  }

  /**
   * getHourTaskNum .
   *
   * @param ownerId ownerId
   * @param groupIds groupIds
   * @return count map
   */
  @RequestMapping(value = "/exec/hourcount", method = RequestMethod.GET)
  public BaseResult getHourTaskNum(
        @RequestParam(value = "ownerId") long ownerId,
        @RequestParam(value = "groupIds") String groupIds
  ) {
    List<Long> ids = new ArrayList<>();
    if (!Strings.isNullOrEmpty(groupIds)) {
      ids = Arrays.stream(groupIds.trim().split(",")).map(str -> {
        return Long.valueOf(str);
      }).collect(Collectors.toList());
    }

    Date now = new Date();
    Calendar startDate = Calendar.getInstance();
    startDate.setTime(now);
    startDate.add(Calendar.HOUR_OF_DAY, -24);
    startDate.set(Calendar.MINUTE, 00);
    startDate.set(Calendar.SECOND, 00);

    Calendar endDate = Calendar.getInstance();
    endDate.setTime(now);
    endDate.add(Calendar.HOUR_OF_DAY, -1);
    endDate.set(Calendar.MINUTE, 59);
    endDate.set(Calendar.SECOND, 59);

    Date startTime = startDate.getTime();
    Date endTime = endDate.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
    logger.info(sdf.format(startTime) + "--->" + sdf.format(endTime));
    List<TaskStaticObj> res =
          taskStatisticService.getHourTaskCount(ownerId, ids,
                sdf.format(startTime), sdf.format(endTime));
    List<Map<String, TaskStaticObj>> resultDataList =
          new ArrayList<>();
    if (null != res && res.size() > 0) {
      for (TaskStaticObj taskStaticObj : res) {
        Map<String, TaskStaticObj> resultData = new HashMap<>();
        resultData.put(taskStaticObj.getTime(), taskStaticObj);
        resultDataList.add(resultData);
      }
    }
    BaseResult result = new BaseResult<>();
    result.setData(resultDataList);
    result.setSuccess(true);

    return result;
  }

  /**
   * getWeekTaskNum .
   *
   * @param ownerId ownerId
   * @param groupIds groupIds
   * @return count map
   */
  @RequestMapping(value = "/exec/weekcount", method = RequestMethod.GET)
  public BaseResult getWeekTaskNum(
        @RequestParam(value = "ownerId") long ownerId,
        @RequestParam(value = "groupIds") String groupIds
  ) {
    List<Long> ids = new ArrayList<>();
    if (!Strings.isNullOrEmpty(groupIds)) {
      ids = Arrays.stream(groupIds.trim().split(",")).map(str -> {
        return Long.valueOf(str);
      }).collect(Collectors.toList());
    }

    Calendar startDate = Calendar.getInstance();
    startDate.add(Calendar.DATE, -7);
    startDate.set(Calendar.HOUR_OF_DAY, 00);
    startDate.set(Calendar.MINUTE, 00);
    startDate.set(Calendar.SECOND, 00);

    Calendar endDate = Calendar.getInstance();
    endDate.add(Calendar.HOUR_OF_DAY, -1);
    endDate.set(Calendar.MINUTE, 59);
    endDate.set(Calendar.SECOND, 59);

    Date startTime = startDate.getTime();
    Date endTime = endDate.getTime();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
    logger.info(sdf.format(startTime) + "--->" + sdf.format(endTime));

    List<TaskStaticObj> res =
          taskStatisticService.getWeekTaskCount(ownerId, ids,
                sdf.format(startTime), sdf.format(endTime));
    List<Map<String, TaskStaticObj>> resultDataList =
          new ArrayList<>();
    if (null != res && res.size() > 0) {
      for (TaskStaticObj taskStaticObj : res) {
        Map<String, TaskStaticObj> resultData = new HashMap<>();
        resultData.put(taskStaticObj.getTime(), taskStaticObj);
        resultDataList.add(resultData);
      }
    }
    BaseResult result = new BaseResult<>();
    result.setData(resultDataList);
    result.setSuccess(true);
    return result;

  }

  /**
   * 获取失败任务数 .
   *
   * @param ownerId ownerId
   * @param groupIds groupIds
   * @param condition condition
   * @return count
   */
  @RequestMapping(value = "/exec/failedcount", method = RequestMethod.GET)
  public BaseResult getFailedTaskNum(
        @RequestParam(value = "ownerId") long ownerId,
        @RequestParam(value = "groupIds") String groupIds,
        @RequestParam(value = "condition") String condition
  ) {

    List<Long> ids = new ArrayList<>();
    if (!Strings.isNullOrEmpty(groupIds)) {
      ids = Arrays.stream(groupIds.trim().split(",")).map(str -> {
        return Long.valueOf(str);
      }).collect(Collectors.toList());
    }

    Calendar startDate = Calendar.getInstance();
    startDate.set(Calendar.MINUTE, 00);
    startDate.set(Calendar.SECOND, 00);

    Date startTime = null;

    String format = "";
    //HOUR 代表统计12小时
    if ("HOUR".equals(condition)) {
      startDate.add(Calendar.HOUR_OF_DAY, -12);
      startTime = startDate.getTime();
      format = "%m%d%H";
    } else if ("DAY".equals(condition)) {
      startDate.add(Calendar.HOUR_OF_DAY, -24);
      startTime = startDate.getTime();
      format = "%m%d%H";
    } else if ("WEEK".equals(condition)) {
      startDate.add(Calendar.HOUR_OF_DAY, -168);
      startTime = startDate.getTime();
      format = "%m%d";
    }

    Calendar endDate = Calendar.getInstance();
    endDate.add(Calendar.HOUR_OF_DAY, -1);
    endDate.set(Calendar.MINUTE, 59);
    endDate.set(Calendar.SECOND, 59);
    Date endTime = endDate.getTime();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
    logger.info(sdf.format(startTime) + "--->" + sdf.format(endTime));

    List<TaskFailedObj> res =
          taskStatisticService.getFailedTaskCount(ownerId, ids,
                sdf.format(startTime), sdf.format(endTime), format);
    List<Map<String, TaskFailedObj>> resultDataList =
          new ArrayList<>();
    if (null != res && res.size() > 0) {
      for (TaskFailedObj taskExecFailedCount : res) {
        Map<String, TaskFailedObj> resultData = new HashMap<>();
        resultData.put(taskExecFailedCount.getTaskName(), taskExecFailedCount);
        resultDataList.add(resultData);
      }
    }
    BaseResult result = new BaseResult<>();
    result.setData(resultDataList);
    result.setSuccess(true);
    return result;

  }

  /**
   * getExecTaskNum .
   *
   * @param ownerId ownerId
   * @param groupIds groupIds
   * @return count map
   */
  @RequestMapping(value = "/exec/allcount", method = RequestMethod.GET)
  public BaseResult getExecAll(
        @RequestParam(value = "ownerId") long ownerId,
        @RequestParam(value = "groupIds") String groupIds
  ) {
    List<Long> ids = new ArrayList<>();
    if (!Strings.isNullOrEmpty(groupIds)) {
      ids = Arrays.stream(groupIds.trim().split(",")).map(str -> {
        return Long.valueOf(str);
      }).collect(Collectors.toList());
    }

    Calendar startDate = Calendar.getInstance();
    startDate.add(Calendar.HOUR_OF_DAY, -168);

    Calendar endDate = Calendar.getInstance();
    endDate.add(Calendar.DATE, -1);
    Date startTime = startDate.getTime();
    Date endTime = endDate.getTime();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    logger.info(sdf.format(startTime) + "--->" + sdf.format(endTime));

    //查询日统计表

    List<TaskExecCount> res = taskStatisticService.getExecDayCount(
          ownerId, ids, sdf.format(startTime), sdf.format(endTime));

    List<Map<String, TaskExecCount>> resultDataList =
          new ArrayList<>();
    if (null != res && res.size() > 0) {
      for (TaskExecCount taskExecCount : res) {
        Map<String, TaskExecCount> resultData = new HashMap<>();
        resultData.put(taskExecCount.getTime(), taskExecCount);
        resultDataList.add(resultData);
      }
    }
    BaseResult result = new BaseResult<>();
    result.setData(resultDataList);
    result.setSuccess(true);
    return result;
  }

}
